package AIRProject.iconmanager
{
	import flash.desktop.ClipboardFormats;
	import flash.desktop.DockIcon;
	import flash.desktop.InteractiveIcon;
	import flash.desktop.NativeApplication;
	import flash.desktop.NativeDragManager;
	import flash.desktop.SystemTrayIcon;
	import flash.display.Loader;
	import flash.display.NativeWindow;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import mx.controls.FlexNativeMenu;
	import mx.events.FlexEvent;
	import mx.events.FlexNativeMenuEvent;
	public class IconManager
	{
		private var iconMenu:FlexNativeMenu; 
		private var icon:Loader = new Loader();
		private var root:Object;
		private var menuData:XML;
		private var icon_url:String;
      /*
      *   s:应用主文档
      *   b:自定义菜单
      */
		public function IconManager(s:Object,b:XML,ico:String)
		{
			root=s;
			menuData = b;
			icon_url = ico;
			
			startWork();
		}
		
		public function startWork():void
		{
			NativeApplication.nativeApplication.autoExit = true;
			iconMenu = new FlexNativeMenu(); 
			iconMenu.dataProvider = menuData; 
			iconMenu.labelField = "@label"; 
			iconMenu.showRoot = false; 
			if (NativeApplication.supportsSystemTrayIcon) {
				
				icon.contentLoaderInfo.addEventListener(Event.COMPLETE, iconLoadComplete);
				
				icon.load(new URLRequest(icon_url));//图标
				root.systemTrayIconMenu = iconMenu;
				root.systemTrayIconMenu.addEventListener(FlexNativeMenuEvent.ITEM_CLICK,systemExit);
			}
			
			if (NativeApplication.supportsDockIcon){
				icon.contentLoaderInfo.addEventListener(Event.COMPLETE,iconLoadComplete);
				icon.load(new URLRequest(icon_url));//图标
				
				root.dockIconMenu = iconMenu;
			}
		}
		
		private function systemExit(e:FlexNativeMenuEvent):void
		{
			
			NativeApplication.nativeApplication.icon.bitmaps = [];
			NativeApplication.nativeApplication.exit();
			
		}
		
		private function iconLoadComplete(event:Event):void
		{
			NativeApplication.nativeApplication.icon.bitmaps =[event.target.content.bitmapData];
		}

	}
}